public class LoanCalculator {
    private double principal; // 贷款总额（万元）
    private int months; // 贷款时间（个月）
    private double annualInterestRate; // 年利率
    private double monthlyInterestRate; // 月利率

    // 构造方法
    public LoanCalculator(double principal, int months, double annualInterestRate) {
        this.principal = principal;
        this.months = months;
        this.annualInterestRate = annualInterestRate;
        this.monthlyInterestRate = annualInterestRate / 12 / 100; // 转换为小数
    }

    // 计算等额本金还款法下的总利息
    public double calculateEqualPrincipalInterest() {
        return (months + 1) * principal * monthlyInterestRate / 2;
    }

    // 计算等额本息还款法下的总利息
    public double calculateEqualPrincipalAndInterest() {
        double numerator = months * principal * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, months);
        double denominator = Math.pow(1 + monthlyInterestRate, months) - 1;

        // 防止分母为0
        if (denominator == 0) {
            throw new ArithmeticException("月利率不能为0，请检查年利率的输入。");
        }

        return numerator / denominator - principal;
    }

    public static void main(String[] args) {
        double a = 100; // 贷款总额（万元）
        int m = 12;     // 贷款时间（个月）
        double annualRate = 5; // 年利率（百分比）

        LoanCalculator calculator = new LoanCalculator(a, m, annualRate);

        double totalInterestEqualPrincipal = calculator.calculateEqualPrincipalInterest();
        System.out.println("等额本金还款法下的总利息: " + totalInterestEqualPrincipal + " 万元");

        double totalInterestEqualPrincipalAndInterest = calculator.calculateEqualPrincipalAndInterest();
        System.out.println("等额本息还款法下的总利息: " + totalInterestEqualPrincipalAndInterest + " 万元");
    }
}